<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加控制箱' : '编辑控制箱'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">

        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称：" prop="companyId" label-width="150px">
              <el-cascader v-model="ruleForm.companyId" placeholder="请选择公司名称" :options="companyArray" :props="{
                  checkStrictly: true,
                  value: 'id',
                  label: 'name',
                  children: 'childList',
                }" @change="changeTreeItem" clearable :disabled="seeType"></el-cascader>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="基地名称：" prop="baseId" label-width="150px">
              <el-select v-model="ruleForm.baseId" placeholder="请选择基地名称" clearable>
                <el-option v-for="item in baseArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像头名称：" prop="name" label-width="150px">
              <el-input v-model="ruleForm.name" placeholder="请输入摄像头名称" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="m3u8播放地址：" prop="m3u8Url" label-width="150px">
              <el-input v-model="ruleForm.m3u8Url" placeholder="请输入m3u8播放地址" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号：" prop="username" label-width="150px">
              <el-input v-model="ruleForm.username" placeholder="请输入账号" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="密码：" prop="passwd" label-width="150px">
              <el-input v-model="ruleForm.passwd" placeholder="请输入密码" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="ip地址：" prop="videoIp" label-width="150px">
              <el-input v-model="ruleForm.videoIp" placeholder="请输入ip地址" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口号：" prop="port" label-width="150px">
              <el-input v-model="ruleForm.port" placeholder="请输入端口号" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像头编号：" prop="code" label-width="150px">
              <el-input v-model="ruleForm.code" placeholder="请输入摄像头编号" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="萤石直播地址：" prop="deviceUrl" label-width="150px">
              <el-input v-model="ruleForm.deviceUrl" placeholder="请输入萤石直播地址" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="萤石云通道号：" prop="ysAppChannelNo" label-width="150px">
              <el-input v-model="ruleForm.ysAppChannelNo" placeholder="请输入萤石云通道号" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="萤石设备序列号：" prop="ysAppCode" label-width="150px">
              <el-input v-model="ruleForm.ysAppCode" placeholder="请输入萤石设备序列号" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="萤石appkey：" prop="ysAppKey" label-width="150px">
              <el-input v-model="ruleForm.ysAppKey" placeholder="请输入萤石appkey" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="萤石密钥：" prop="ysAppSecret" label-width="150px">
              <el-input v-model="ruleForm.ysAppSecret" placeholder="请输入萤石密钥" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import md5 from 'js-md5'
import { getObjDeviceVideo, saveObjDeviceVideo } from '@/api/home'
import { getSysCompanyBases } from '@/api/system'

import { getCompanyInfoTree } from '@/api/company'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        baseId: '',
        code: '',
        companyId: '',
        deviceUrl: '',
        id: '',
        m3u8Url: '',
        name: '',
        ysAppChannelNo: '',
        ysAppCode: '',
        ysAppKey: '',
        ysAppSecret: ''
      },
      rules: {
        // 表单规则
        companyId: [{ required: true, message: '请选择公司名称', trigger: 'change' }],
        baseId: [{ required: true, message: '请选择基地名称', trigger: 'change' }],
        name: [{ required: true, message: '请输入摄像头名称', trigger: 'blur' }],
        ysAppChannelNo: [{ required: true, message: '请输入萤石云通道号', trigger: 'blur' }],
        ysAppCode: [{ required: true, message: '请输入萤石设备序列号', trigger: 'blur' }],
        ysAppKey: [{ required: true, message: '请输入萤石appkey', trigger: 'blur' }],
        ysAppSecret: [{ required: true, message: '请输入萤石密钥', trigger: 'blur' }],
        m3u8Url: [{ required: true, message: '请输入m3u8转vnr播放地址', trigger: 'blur' }],
        passwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        port: [{ required: true, message: '请输入端口号', trigger: 'blur' }],
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入摄像头编号', trigger: 'blur' }],
        deviceUrl: [{ required: true, message: '请输入萤石直播地址', trigger: 'blur' }],
        videoIp: [{ required: true, message: '请输入ip地址', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      baseArray: [], // 角色集合
      companyArray: [], // 公司集合
      userType: '' // 用户类型（0超级管理员，1公司管理员）
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
          this.getCompanyInfoTree()
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        this.userType = Number(window.localStorage.getItem('userType'))
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getObjDeviceVideo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data

          // 获取公司列表
          this.getCompanyInfoTree()

          this.getSysCompanyBases()
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveObjDeviceVideo(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // 获取公司列表
    getCompanyInfoTree() {
      if (this.ruleForm.userType === '') {
        return
      }

      if (this.ruleForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: '总公司',
            parentId: 0
          }
        ]
      } else {
        getCompanyInfoTree({}).then((res) => {
          if (res.code == 200) {
            console.log(222)
            this.companyArray = res.data
          }
        })
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1]
      } else {
        this.ruleForm.companyId = ''
      }

      this.ruleForm.baseId = ''
      this.baseArray = []

      // 获取角色列表
      this.getSysCompanyBases()
    },
    // 获取角色列表
    getSysCompanyBases() {
      if (this.ruleForm.companyId === '' || this.ruleForm.companyId == null) {
        return
      }

      getSysCompanyBases({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
